if (window.plus) {
    ready();
} else {
    document.addEventListener('plusready', ready, false);
}

$(function(){
  if(AGENT_CLIENT.WEB){
    ready();
  } 
})

function ready() {
    var user = null, 
    html = [], 
    thisPage = null,
    currentLoginUser = {};

    var bindCreditcardPage = function () {
        this.init();
    };
    bindCreditcardPage.prototype = {
        init: function () {
            this.initData();
            this.bindEvent();
        },
        initData: function () {
            user = getLocalStorage(STORAGE_MANAGER.user);
            if (!user || !user.id) {
                redirect("../account/login.html");
                return false;
            }
            //通过用户ID发送验证码
			$("#getVcode").attr("tip", user.id);
//          var query = {
//              index: 1,
//              size: 10,
//              query: "",
//              search: {},
//              sorting: []
//          };
            //如果已经绑定银行账号
//          $.ajax({
//              type: "POST",
//              url: CONFIG_MANAGER.RequstUrl + "v1/bank/" + user.id,
//              async: true,
//              headers: {token: user.token},
//              data: query,
//          }).done(function (data) {
//				var data = JSON.parse(data),card = data.data[0];
//				$("#bank_account").val(card.bank_account);
//				$("#bank_name").val(card.bank_name);
//				$("#bank_no").val(card.bank_no);
//          }).fail(function (data) {
//              var infor = JSON.parse(data);
//              $.alert(infor.message);
//          });
        },
        bindEvent: function () {
            //点击后退
            $("#back").on("click", function () { back(); });
            //点击删除
            $("#clear").on("click", function () {
                $("input").val("");
            });
            //点击获取验证码
            $("#getVcode").on("click", function () {
                change_code(this, this, SMS_TYPE.safety);
            });

            $("#bank_account,#bank_name,#bank_no").on("input", function () {

                if ("" == $("#bank_account").val() ||
                    "" == $("#bank_name").val() ||
                    "" == $("#bank_no").val()) {
                    $("#bindCard").addClass("gray_color").unbind();
                } else {
                    $("#bindCard").removeClass("gray_color").unbind().on("click", function () {
                        if (!isChineseName($("#bank_account").val())) {
                            $.alert("请正确填写姓名");
                            return false;
                        }
                        if (!isChineseCharacter($("#bank_name").val(), 4, 50)) {
                            $.alert("请正确填写开户行");
                            return false;
                        }
                        if (!cardCheck($("#bank_no").val())) {
                            $.alert("请正确填写银行卡号");
                            return false;
                        }
						if(!isVCode($("#vcode").val())){
							$.alert("请填写正确的验证码");
							return false;              	
						}

                        var requestData = {
                            bank_account: $("#bank_account").val(),
                            bank_name: $("#bank_name").val(),
                            bank_no: $("#bank_no").val(),
                            use_flag: 1,
                            vcode: $("#vcode").val(),
                        }
                        $.ajax({
                            type: "POST",
                            url: CONFIG_MANAGER.RequstUrl + "v1/bank/bind/" + user.id,
                            async: true,
                            contentType: 'application/json',
                            data: JSON.stringify(requestData),
                            headers: {"token": user.token},
                        }).done(function (data) {
                            $.alert("绑定成功", function(){
                            	redirect("../mine/account_safe.html"); 
                            });
                        }).fail(function (data) {
                            $.toast(data.responseJSON.Message);
                        });
                    });
                }
            });
        }
    };

    thisPage = new bindCreditcardPage();
}
